﻿namespace Dcms.OleDbDAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.OleDb;

    public class Dcms_ModelFieldTempDAL : IDcms_ModelFieldTempDAL
    {
        public Dcms_ModelFieldTempModel AddModel(Dcms_ModelFieldTempModel model)
        {
            string sQLString = "insert into Dcms_ModelFieldTemp(mf_FieldName,mf_FieldLabel,mf_DisplayFlag,mf_Model,mf_ValidateType,mf_ValidateField,mf_RegexErrorMsg,mf_MustField,mf_Order,mf_FieldInfo,mf_FieldHelpInfo,mf_FieldDefaultValue,mf_RequiredField,mf_ModelKeyId) values(@mf_FieldName,@mf_FieldLabel,@mf_DisplayFlag,@mf_Model,@mf_ValidateType,@mf_ValidateField,@mf_RegexErrorMsg,@mf_MustField,@mf_Order,@mf_FieldInfo,@mf_FieldHelpInfo,@mf_FieldDefaultValue,@mf_RequiredField,@mf_ModelKeyId)";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@mf_FieldName", OleDbType.VarChar), new OleDbParameter("@mf_FieldLabel", OleDbType.VarChar), new OleDbParameter("@mf_DisplayFlag", OleDbType.SmallInt), new OleDbParameter("@mf_Model", OleDbType.VarChar), new OleDbParameter("@mf_ValidateType", OleDbType.SmallInt), new OleDbParameter("@mf_ValidateField", OleDbType.VarChar), new OleDbParameter("@mf_RegexErrorMsg", OleDbType.VarChar), new OleDbParameter("@mf_MustField", OleDbType.SmallInt), new OleDbParameter("@mf_Order", OleDbType.SmallInt), new OleDbParameter("@mf_FieldInfo", OleDbType.LongVarChar), new OleDbParameter("@mf_FieldHelpInfo", OleDbType.VarChar), new OleDbParameter("@mf_FieldDefaultValue", OleDbType.VarChar), new OleDbParameter("@mf_RequiredField", OleDbType.SmallInt), new OleDbParameter("@mf_ModelKeyId", OleDbType.SmallInt) };
            cmdParms[0].Value = model.mf_FieldName;
            cmdParms[1].Value = model.mf_FieldLabel;
            cmdParms[2].Value = model.mf_DisplayFlag;
            cmdParms[3].Value = model.mf_Model;
            cmdParms[4].Value = model.mf_ValidateType;
            cmdParms[5].Value = model.mf_ValidateField;
            cmdParms[6].Value = model.mf_RegexErrorMsg;
            cmdParms[7].Value = model.mf_MustField;
            cmdParms[8].Value = model.mf_Order;
            cmdParms[9].Value = model.mf_FieldInfo;
            cmdParms[10].Value = model.mf_FieldHelpInfo;
            cmdParms[11].Value = model.mf_FieldDefaultValue;
            cmdParms[12].Value = model.mf_RequiredField;
            cmdParms[13].Value = model.mf_ModelKeyId;
            model.mf_FieldID = OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public IList<Dcms_ModelFieldTempModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_ModelFieldTempModel> list = new List<Dcms_ModelFieldTempModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_ModelFieldTempModel item = new Dcms_ModelFieldTempModel();
                if (ds.Tables[0].Rows[i]["mf_FieldID"].ToString() != "") item.mf_FieldID = int.Parse(ds.Tables[0].Rows[i]["mf_FieldID"].ToString());
                item.mf_FieldName = ds.Tables[0].Rows[i]["mf_FieldName"].ToString();
                item.mf_FieldLabel = ds.Tables[0].Rows[i]["mf_FieldLabel"].ToString();
                if (ds.Tables[0].Rows[i]["mf_DisplayFlag"].ToString() != "") item.mf_DisplayFlag = int.Parse(ds.Tables[0].Rows[i]["mf_DisplayFlag"].ToString());
                item.mf_Model = ds.Tables[0].Rows[i]["mf_Model"].ToString();
                if (ds.Tables[0].Rows[i]["mf_ValidateType"].ToString() != "") item.mf_ValidateType = int.Parse(ds.Tables[0].Rows[i]["mf_ValidateType"].ToString());
                item.mf_ValidateField = ds.Tables[0].Rows[i]["mf_ValidateField"].ToString();
                item.mf_RegexErrorMsg = ds.Tables[0].Rows[i]["mf_RegexErrorMsg"].ToString();
                if (ds.Tables[0].Rows[i]["mf_MustField"].ToString() != "") item.mf_MustField = int.Parse(ds.Tables[0].Rows[i]["mf_MustField"].ToString());
                if (ds.Tables[0].Rows[i]["mf_Order"].ToString() != "") item.mf_Order = int.Parse(ds.Tables[0].Rows[i]["mf_Order"].ToString());
                item.mf_FieldInfo = ds.Tables[0].Rows[i]["mf_FieldInfo"].ToString();
                item.mf_FieldHelpInfo = ds.Tables[0].Rows[i]["mf_FieldHelpInfo"].ToString();
                item.mf_FieldDefaultValue = ds.Tables[0].Rows[i]["mf_FieldDefaultValue"].ToString();
                if (ds.Tables[0].Rows[i]["mf_RequiredField"].ToString() != "") item.mf_RequiredField = int.Parse(ds.Tables[0].Rows[i]["mf_RequiredField"].ToString());
                if (ds.Tables[0].Rows[i]["mf_ModelKeyId"].ToString() != "") item.mf_ModelKeyId = int.Parse(ds.Tables[0].Rows[i]["mf_ModelKeyId"].ToString());
                list.Add(item);
            }
            return list;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_ModelFieldTemp where mf_FieldID=@mf_FieldID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@mf_FieldID", OleDbType.Integer) };
            cmdParms[0].Value = ID;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
        }

        public Dcms_ModelFieldTempModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_ModelFieldTemp where mf_FieldID=@mf_FieldID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@mf_FieldID", OleDbType.Integer) };
            cmdParms[0].Value = ID;
            Dcms_ModelFieldTempModel model = new Dcms_ModelFieldTempModel();
            DataSet ds = OleDbHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_ModelFieldTempModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_ModelFieldTemp";
            return OleDbHelper.CreateDataSet(sql);
        }

        public Dcms_ModelFieldTempModel Update(Dcms_ModelFieldTempModel model)
        {
            string sQLString = "update Dcms_ModelFieldTemp set mf_FieldName=@mf_FieldName,mf_FieldLabel=@mf_FieldLabel,mf_DisplayFlag=@mf_DisplayFlag,mf_Model=@mf_Model,mf_ValidateType=@mf_ValidateType,mf_ValidateField=@mf_ValidateField,mf_RegexErrorMsg=@mf_RegexErrorMsg,mf_MustField=@mf_MustField,mf_Order=@mf_Order,mf_FieldInfo=@mf_FieldInfo,mf_FieldHelpInfo=@mf_FieldHelpInfo,mf_FieldDefaultValue=@mf_FieldDefaultValue,mf_RequiredField=@mf_RequiredField,mf_ModelKeyId=@mf_ModelKeyId where mf_FieldID=@mf_FieldID";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@mf_FieldName", OleDbType.VarChar), new OleDbParameter("@mf_FieldLabel", OleDbType.VarChar), new OleDbParameter("@mf_DisplayFlag", OleDbType.SmallInt), new OleDbParameter("@mf_Model", OleDbType.VarChar), new OleDbParameter("@mf_ValidateType", OleDbType.SmallInt), new OleDbParameter("@mf_ValidateField", OleDbType.VarChar), new OleDbParameter("@mf_RegexErrorMsg", OleDbType.VarChar), new OleDbParameter("@mf_MustField", OleDbType.SmallInt), new OleDbParameter("@mf_Order", OleDbType.SmallInt), new OleDbParameter("@mf_FieldInfo", OleDbType.LongVarChar), new OleDbParameter("@mf_FieldHelpInfo", OleDbType.VarChar), new OleDbParameter("@mf_FieldDefaultValue", OleDbType.VarChar), new OleDbParameter("@mf_RequiredField", OleDbType.SmallInt), new OleDbParameter("@mf_ModelKeyId", OleDbType.SmallInt), new OleDbParameter("@mf_FieldID", OleDbType.Integer) };
            cmdParms[0].Value = model.mf_FieldName;
            cmdParms[1].Value = model.mf_FieldLabel;
            cmdParms[2].Value = model.mf_DisplayFlag;
            cmdParms[3].Value = model.mf_Model;
            cmdParms[4].Value = model.mf_ValidateType;
            cmdParms[5].Value = model.mf_ValidateField;
            cmdParms[6].Value = model.mf_RegexErrorMsg;
            cmdParms[7].Value = model.mf_MustField;
            cmdParms[8].Value = model.mf_Order;
            cmdParms[9].Value = model.mf_FieldInfo;
            cmdParms[10].Value = model.mf_FieldHelpInfo;
            cmdParms[11].Value = model.mf_FieldDefaultValue;
            cmdParms[12].Value = model.mf_RequiredField;
            cmdParms[13].Value = model.mf_ModelKeyId;
            cmdParms[14].Value = model.mf_FieldID;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }
    }
}

